﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;
using Windows.Data.Json;

namespace XiaoweiVoiceAI.ResponseCreators
{
    public static class WeatherResponseCreator
    {
        private class WeatherData : IWeatherData
        {
            public WeatherCondition Condition {get; set;}

            public string Date {get; set;}

            public string MaxTemperature {get; set;}

            public string MinTemperature {get; set;}

            public string Temperature {get; set;}

            public string PM25 {get; set;}

            public string Quality {get; set;}

            public string WindDirection {get; set;}

            public string WindLevel {get; set;}
        }

        private class WeatherResponseData : IWeatherResponseData
        {
            public string Location { get; set; }

            public IReadOnlyList<IWeatherData> WeatherInfoList { get; set; }

            public int CurrentInfo { get; set; }

            public ResponseType ResponseType => ResponseType.Weather;

            public ErrorCode ErrorCode => ErrorCode.NoError;

            public string RequestText { get; set; }
            public string ResponseText { get; set; }
            public string TTSUrl { get; set; }

            public bool RestoreInHistory => true;

            public object DataContext { get; set; }
        }

        private static readonly Dictionary<string, WeatherCondition> TextAndEnumMap = new Dictionary<string, WeatherCondition>()
        {
            {"晴",WeatherCondition.Sunny},
            {"大部晴朗",WeatherCondition.MostlySunny},
            {"多云",WeatherCondition.Cloudy},
            {"少云",WeatherCondition.PartlyCloudy},
            {"阴",WeatherCondition.Overcast},
            {"阵雨",WeatherCondition.Shower},
            {"局部阵雨",WeatherCondition.PartialShower},
            {"小阵雨",WeatherCondition.SmallShower},
            {"强阵雨",WeatherCondition.StrongShower},
            {"阵雪",WeatherCondition.SnowShower},
            {"小阵雪",WeatherCondition.SmallSnowShower},
            {"雾",WeatherCondition.Fog},
            {"冻雾",WeatherCondition.FrozenFog},
            {"沙尘暴",WeatherCondition.Sandstorm},
            {"浮尘",WeatherCondition.FloatingDust},
            {"尘卷风",WeatherCondition.DustyWind},
            {"扬沙",WeatherCondition.BlowingSand},
            {"强沙尘暴",WeatherCondition.StrongSandstorm},
            {"霾",WeatherCondition.Haze},
            {"雷阵雨",WeatherCondition.Thundershower},
            {"雷电",WeatherCondition.Raiden},
            {"雷暴",WeatherCondition.Thunderstorm},
            {"雷阵雨伴",WeatherCondition.ThunderShower2},
            {"冰雹",WeatherCondition.Hail},
            {"冰针",WeatherCondition.IceNeedle},
            {"冰粒",WeatherCondition.IceParticle},
            {"雨夹雪",WeatherCondition.Sleet},
            {"小雨",WeatherCondition.LightRain},
            {"中雨",WeatherCondition.ModerateRain},
            {"大雨",WeatherCondition.HeavyRain},
            {"暴雨",WeatherCondition.Rainstorm},
            {"大暴雨",WeatherCondition.HeavyRainstorm},
            {"特大暴雨",WeatherCondition.ExtraHeavyRainstorm},
            {"小雪",WeatherCondition.LightSnow},
            {"中雪",WeatherCondition.ModerateSnow},
            {"大雪",WeatherCondition.HeavySnow},
            {"暴雪",WeatherCondition.Blizzard},
            {"冻雨",WeatherCondition.FreezingRain},
            {"雪",WeatherCondition.Snow},
            {"雨",WeatherCondition.Rain},
            {"小到中雨",WeatherCondition.LightToModerateRain},
            {"中到大雨",WeatherCondition.ModerateToHeavyRain},
            {"大到暴雨",WeatherCondition.HeavyRainToRainStorm},
            {"小到中雪",WeatherCondition.SmallToMediumSnow},
            {"晴_晚上",WeatherCondition.SunnyNight},
            {"大部晴朗_晚上",WeatherCondition.MostlySunnyNight},
            {"多云_晚上",WeatherCondition.CloudyNight},
            {"少云_晚上",WeatherCondition.PartlyCloudyNight},
            {"阴_晚上",WeatherCondition.OvercastNight},
            {"阵雨_晚上",WeatherCondition.ShowerNight},
            {"局部阵雨_晚上",WeatherCondition.PartialShowerNight},
            {"小阵雨_晚上",WeatherCondition.SmallShowerNight},
            {"强阵雨_晚上",WeatherCondition.StrongShowerNight},
            {"阵雪_晚上",WeatherCondition.SnowShowerNight},
            {"小阵雪_晚上",WeatherCondition.SmallSnowShowerNight},
            {"雾_晚上",WeatherCondition.FogNight},
            {"冻雾_晚上",WeatherCondition.FrozenFogNight},
            {"沙尘暴_晚上",WeatherCondition.SandstormNight},
            {"浮尘_晚上",WeatherCondition.FloatingDustNight},
            {"尘卷风_晚上",WeatherCondition.DustyWindNight},
            {"扬沙_晚上",WeatherCondition.BlowingSandNight},
            {"强沙尘暴_晚上",WeatherCondition.StrongSandstormNight},
            {"霾_晚上",WeatherCondition.HazeNight},
            {"雷阵雨_晚上",WeatherCondition.ThundershowerNight},
            {"雷电_晚上",WeatherCondition.RaidenNight},
            {"雷暴_晚上",WeatherCondition.ThunderstormNight},
            {"雷阵雨伴_晚上",WeatherCondition.ThunderShower2Night},
            {"冰雹_晚上",WeatherCondition.HailNight},
            {"冰针_晚上",WeatherCondition.IceNeedleNight},
            {"冰粒_晚上",WeatherCondition.IceGrainNight},
            {"雨夹雪_晚上",WeatherCondition.SleetNight},
            {"小雨_晚上",WeatherCondition.LightRainNight},
            {"中雨_晚上",WeatherCondition.ModerateRainNight},
            {"大雨_晚上",WeatherCondition.HeavyRainNight},
            {"暴雨_晚上",WeatherCondition.RainstormNight},
            {"大暴雨_晚上",WeatherCondition.HeavyRainstormNight},
            {"特大暴雨_晚上",WeatherCondition.ExtremelyHeavyRainNight},
            {"小雪_晚上",WeatherCondition.LightSnowNight},
            {"中雪_晚上",WeatherCondition.ModerateSnowNight},
            {"大雪_晚上",WeatherCondition.HeavySnowNight},
            {"暴雪_晚上",WeatherCondition.BlizzardNight},
            {"冻雨_晚上",WeatherCondition.FreezingRainnight},
            {"雪_晚上",WeatherCondition.SnowNight},
            {"雨_晚上",WeatherCondition.RainNight},
            {"小到中雨_晚上",WeatherCondition.SmallToModerateRainNight},
            {"中到大雨_晚上",WeatherCondition.ModerateToHeavyRainNight},
            {"大到暴雨_晚上",WeatherCondition.HeavyRainToRainStormNight},
            {"小到中雪_晚上",WeatherCondition.SmallToMediumSnowNight}
        };

        public static IResponseData CreateResponseData(XiaoweiVoiceAI xiaoweiVoiceAI, XiaoweiV2.ResponseParam param)
        {
            if (String.IsNullOrEmpty(param.ResponseData)) return null;
            WeatherResponseData weatherResponseData = new WeatherResponseData();
            try
            {
                JsonObject jsonObject = JsonObject.Parse(param.ResponseData);
                weatherResponseData.Location = jsonObject.GetNamedString("loc", "");
                List<IWeatherData> InfoList = new List<IWeatherData>();
                weatherResponseData.CurrentInfo = -1;
                foreach (IJsonValue jsonValue in jsonObject.GetNamedArray("data"))
                {
                    if (jsonValue.ValueType == JsonValueType.Object)
                    {
                        JsonObject jsonObject2 = jsonValue.GetObject();
                        WeatherData weatherData = new WeatherData();
                        string condition = jsonObject2.GetNamedString("condition", "");
                        if(TextAndEnumMap.ContainsKey(condition))
                        {
                            weatherData.Condition = TextAndEnumMap[condition];
                        }
                        else
                        {
                            weatherData.Condition = WeatherCondition.Error;
                        }
                        weatherData.Date = jsonObject2.GetNamedString("date", "");
                        bool isAsked = jsonObject2.GetNamedString("is_asked", "") == "1" ? true : false;
                        weatherData.MaxTemperature = jsonObject2.GetNamedString("max_tp", "");
                        weatherData.MinTemperature = jsonObject2.GetNamedString("min_tp", "");
                        weatherData.Temperature = jsonObject2.GetNamedString("tp", "");
                        weatherData.PM25 = jsonObject2.GetNamedString("pm25", "");
                        weatherData.Quality = jsonObject2.GetNamedString("quality", "");
                        weatherData.WindDirection = jsonObject2.GetNamedString("wind_direct", "");
                        weatherData.WindLevel = jsonObject2.GetNamedString("wind_lv", "");
                        InfoList.Add(weatherData);
                        if (isAsked)
                        {
                            weatherResponseData.CurrentInfo = InfoList.Count - 1;
                        }
                    }
                }
                weatherResponseData.WeatherInfoList = InfoList;
            }
            catch
            {
                return null;
            }
            weatherResponseData.ResponseText = param.ResponseText;
            weatherResponseData.RequestText = param.RequestText;
            weatherResponseData.TTSUrl = XiaoweiV2.ResponseHelper.GetTTSUrl(param);
            return weatherResponseData;
        }
    }
}
